package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import com.itheima.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author ch200
 * @description 针对表【student(学员表)】的数据库操作Service实现
 * @createDate 2025-03-11 20:34:52
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    //分页查询
    @Override
    public PageResult pageQuery(StudentQueryParam studentQueryParam) {
        //1.设置分页参数
        Integer page = studentQueryParam.getPage();
        Integer pageSize = studentQueryParam.getPageSize();
        //2.创建分页对象
        Page<Student> p = new Page<>(page, pageSize);
        //3.创建条件对象

        QueryWrapper<Student> wrapper = new QueryWrapper<>();

        if (studentQueryParam.getName() != null && !studentQueryParam.getName().trim().isEmpty()) {
            wrapper.like("s.name", studentQueryParam.getName());
        }
        if (studentQueryParam.getDegree() != null) {
            wrapper.eq("s.degree", studentQueryParam.getDegree());
        }
        if (studentQueryParam.getClazzId() != null) {
            wrapper.eq("s.clazzId", studentQueryParam.getClazzId());
        }
        wrapper.orderByDesc("s.update_time");
        //4.调用方法
        studentMapper.page(p, wrapper);
        return new PageResult(p.getTotal(), p.getRecords());
    }

    //批量删除
    @Override
    public void delete(Integer[] ids) {
        lambdaUpdate()
                .in(Student::getId, ids)
                .remove();
    }


    //添加学员
    @Override
    public void MySave(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);


    }


    //    该接口用于根据主键ID查询学员的信息
    @Override
    public Student getByIdInfo(Integer id) {

//        Student student = lambdaQuery().select(Student::getId, Student::getName, Student::getNo, Student::getGender, Student::getPhone, Student::getIdCard, Student::getIsCollege, Student::getAddress, Student::getDegree, Student::getGraduationDate, Student::getClazzId, Student::getViolationCount, Student::getViolationScore, Student::getClazzId, Student::getCreateTime, Student::getUpdateTime)
//                .eq(Student::getId, id)
//                .one();

        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "no", "gender", "phone", "id_card", "is_college", "address", "degree", "graduation_date", "clazz_id", "violation_count", "violation_score", "create_time", "update_time")
                .eq("id", id);
        Student student = studentMapper.selectOne(wrapper);
        return student;


    }

    //    违纪处理
    @Override
    public void update(Integer id, Integer score) {
        // 每一次违纪处理 , 就需要将违纪次数往上累加一次 。
        //每一次违纪处理 , 需要将违纪分数累加。

        LambdaUpdateWrapper<Student> wrapper = new LambdaUpdateWrapper<>();

        wrapper.eq(Student::getId, id)
                .setSql("violation_count=violation_count+1")
                .setSql("violation_score=violation_score+" + score)
                .set(Student::getUpdateTime, LocalDateTime.now());


        studentMapper.update(null, wrapper);


    }
}




